Le module math

Pour importer le module math

In [2]:
import math

Les multiplications

Ne pas oublier les multiplications entre les constantes et les variables.

In [4]:
#  2x+3 n'est pas accepté
def f(x):
    return 2*x+3
f(5)
Out[4]:
13

Les puissances

In [23]:
def f(x):
    return x**3+x**2
f(5)
Out[23]:
150
In [22]:
def f(x):
    return math.pow(x,3)+math.pow(x,2)
f(5)
Out[22]:
150.0

La racine carrée

In [7]:
# en utilisant le module math
a=math.sqrt(9)
print(a)
3.0
In [8]:
# avec la fonction puissance
a=9**0.5
print(a)
3.0

Les divisions

In [9]:
# la division euclidienne
b=13//5
print(b)
2
In [10]:
# la division décimale
b=13/5
print(b)
2.6

Les fonctions exponentielle et logarithme népérien

In [13]:
# fonction exp
c=math.exp(1)
print(c)
2.718281828459045
In [14]:
#fonction ln 
d=math.log(2)
print(d)
0.6931471805599453
In [15]:
# constante e
e=math.e
print(e)
2.718281828459045
In [16]:
# fonction log de base a
a=math.log(100,10)
print(a)
b=math.log(64,2)
print(b)
2.0
6.0

Les fonctions trigonométriques

Ces fonctions travaillent en radians.

In [21]:
# constante PI
p=math.pi
print(p)
3.141592653589793
In [20]:
# fonctions cos, sin, tan
a=math.cos(0)
print(a)
b=math.sin(math.pi) # presque zéro ...
print(b)
c=math.tan(math.pi/4) # presque 1 ...
print(c)
1.0
1.2246467991473532e-16
0.9999999999999999
In [25]:
# convertir en degrés
a=math.degrees(math.pi)
print(a)
# convertir en radians
b=math.radians(90)
print(b)
180.0
1.5707963267948966

Quelques fonctions arithmétiques

In [28]:
# valeur entière par excès
a=math.ceil(5.8)
print(a)
# valeur entière par défaut
b=math.floor(5.8)
print(b)
6
5
In [30]:
# factoriel
a=math.factorial(5)
print(a)
120
In [32]:
# pgcd
a=math.gcd(18,45)
print(a)
9
In [ ]:
 
In [ ]:
 
In [ ]: